function stringToHex(str){
　　var val="";
　　for(var i = 0; i < str.length; i++){
　　　　if(val == "")
　　　　　　val = str.charCodeAt(i).toString(16);
　　　　else
　　　　　　val += str.charCodeAt(i).toString(16);
　　}
　　return val;
}

function randomString(length) {
	var str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var result = '';
	for (var i = length; i > 0; --i) 
		result += str[Math.floor(Math.random() * str.length)];
	return result;
}

function benchsm2_enc_dec(size) {
	const cipherMode = 0; // 1 - C1C3C2，0 - C1C2C3，默认为1
	msgString = randomString(size);

	// 加密
	let encryptData = "";
	let decryptData = "";

	var start = new Date().getTime();
	for (var i = 0; i < BENCHMARK_TIME; ++i) {
		encryptData = sm2.doEncrypt(msgString, publicKey, cipherMode); // 加密结果
	}
	var end = new Date().getTime();
	var time = end - start;
	console.log("SM2加密[%d]Bytes性能测试（测试次数:%d）:[%f]ms", size, BENCHMARK_TIME, time/BENCHMARK_TIME);

	start = new Date().getTime();
	for (var i = 0; i < BENCHMARK_TIME; ++i) {
		decryptData = sm2.doDecrypt(encryptData, privateKey, cipherMode); // 解密结果
	}
	end = new Date().getTime();
	time = end - start;
	console.log("SM2解密[%d]Bytes性能测试（测试次数:%d）:[%f]ms", size, BENCHMARK_TIME, time/BENCHMARK_TIME);
    //console.log("【encryptData】", encryptData);
    //console.log("【decryptData】", decryptData);
}

function benchsm3_hash(size) {
    const sm3 = require("sm-crypto").sm3;

    let hashData = "";
    msgString = randomString(size);

    var start = new Date().getTime();
    for (var i = 0; i < BENCHMARK_TIME; ++i) {
        hashData = sm3(msgString); // 杂凑hashData
    }
    var end = new Date().getTime();
    var time = end - start;
    console.log("SM3 hash [%d]Bytes性能测试（测试次数:%d）: [%f]ms", size, BENCHMARK_TIME, time/BENCHMARK_TIME);
    //console.log("【hashData】", hashData);
}

function benchsm4_enc_dec(size) {
    let sm4 = require("sm-crypto").sm4;
    msgString = randomString(size); // 可以为 utf8 串或字节数组
    let k = "1234567890123456";
    key = stringToHex(k)
    let encryptData = "";
    let decryptData = "";

    var start = new Date().getTime();
    for (var i = 0; i < BENCHMARK_TIME; ++i) {
        encryptData = sm4.encrypt(msgString, key); // 加密，默认输出 16 进制字符串，默认使用 pkcs#5 填充
    }
    var end = new Date().getTime();
    var time = end - start;
    console.log("SM4 加密[%d]Bytes性能测试（测试次数:%d）: [%f]ms", size, BENCHMARK_TIME, time/BENCHMARK_TIME);

    start = new Date().getTime();
    for (var i = 0; i < BENCHMARK_TIME; ++i) {
        decryptData = sm4.decrypt(encryptData, key); // 解密，默认输出 utf8 字符串，默认使用 pkcs#5 填充
    }
    end = new Date().getTime();
    time = end - start;
    console.log("SM4 加密[%d]Bytes性能测试（测试次数:%d）: [%f]ms", size, BENCHMARK_TIME, time/BENCHMARK_TIME);
    //console.log("【encryptData】", encryptData);
    //console.log("【decryptData】", decryptData);
}

var BENCHMARK_TIME = 10000; // 测试次数
let msgString = "";

///////////////////////////////////////////     SM2     ////////////////////////////////////////
console.log("\n===================================================");
console.log("=====================【sm2】=======================");
console.log("===================================================");
const sm2 = require("sm-crypto").sm2;

let keypair = "";

// 公私钥生成
var start = new Date().getTime(); // Date().getTime获取的是毫秒数
for (var i = 0; i < BENCHMARK_TIME / 10; ++i) {
    keypair = sm2.generateKeyPairHex();
}
var end = new Date().getTime();
var time = end - start;
console.log("SM2 公钥生成性能测试（测试次数:%d）: [%f]ms", BENCHMARK_TIME/10, time/BENCHMARK_TIME*10)

let publicKey = keypair.publicKey
let privateKey = keypair.privateKey

// 签名
let sigValueHex2 = "";
let verifyResult2 = ""; 
msgString = randomString(8);

start = new Date().getTime();
for (var i = 0; i < BENCHMARK_TIME; ++i) {
    sigValueHex2 = sm2.doSignature(msgString, privateKey); // 签名
}
end = new Date().getTime();
time = end - start;
console.log("SM2 签名性能测试（测试次数:%d）: [%f]ms",BENCHMARK_TIME, time/BENCHMARK_TIME);

// 验签 
start = new Date().getTime();
for (var i = 0; i < BENCHMARK_TIME; ++i) {
    verifyResult2 = sm2.doVerifySignature(msgString, sigValueHex2, publicKey); // 验签结果
}
end = new Date().getTime();
time = end - start;
console.log("SM2 验签性能测试（测试次数:%d）: [%f]ms",BENCHMARK_TIME, time/BENCHMARK_TIME);

// 加解密
benchsm2_enc_dec(8);
benchsm2_enc_dec(1024);
benchsm2_enc_dec(8196);

console.log("===================================================");

////////////////////////////////////////     SM3     ////////////////////////////////////////////

console.log("\n===================================================");
console.log("=====================【sm3】=======================");
console.log("===================================================");

benchsm3_hash(8);
benchsm3_hash(1024);
benchsm3_hash(8192);

console.log("===================================================");

////////////////////////////////////////     SM4     ////////////////////////////////////////////

console.log("\n===================================================");
console.log("=====================【sm4】=======================");
console.log("===================================================");

benchsm4_enc_dec(8);
benchsm4_enc_dec(1024);
benchsm4_enc_dec(8192);

console.log("===================================================");
